/*
 * Copyright (C) 2023 Coder.AN
 * Email: an.hongjun@foxmail.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */
#ifndef __XBOX_H__
#define __XBOX_H__


#include <stdio.h>
#include <fcntl.h>
#include <linux/input.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>
#include <iostream>
#include <memory.h>
#include <thread>

#ifndef XBOX_DEV
#define XBOX_DEV        "/dev/input/event7"
#endif


struct XBoxInputVal {
    int X1, Y1, X2, Y2;
    int A, B, X, Y;
    int du, dd, dl, dr;
    int back, guide, start;
    int lt, lb, rt, rb;
};

class XBox
{
private:
    int xboxDev = 0;
    void (*callback)(struct XBoxInputVal);
    struct XBoxInputVal xboxInputValue;
    
    bool work_handle;
    std::thread work_thread;
    void work_func();

    void parseKey(struct input_event ev);
    void parseAbs(struct input_event ev);
    void parseSyn(struct input_event ev);

public:
    XBox(void (*callback_func)(struct XBoxInputVal), const char* dev_name=XBOX_DEV);
    ~XBox();
};

#endif // __XBOX_H__
